package com.swf.seed.jolt;

import bea.jolt.JoltRemoteService;
import bea.jolt.JoltSession;
import bea.jolt.JoltSessionAttributes;

/**
 * <p>Title: 基本Jolt调用的对象</p>
 * <p>Description: 使用JoltRemoteService对象来保存数据，BasicJoltData封装了JoltRemoteService对象</p>
 * <p>Copyright: Copyright (c) 2009</p>
 * <p>Company: Beijing Watchdata CO,.Ltd</p>
 * @author da.wang
 * @version 1.0
 */
public class BasicJoltData implements IJoltData{
    private JoltRemoteService service = null;
    private JoltSessionAttributes attr = null;
    private JoltSession  session = null;
    private String serviceName = null;

    public BasicJoltData( BasicJoltConfig cfg, String serviceName  ) {
        this.serviceName = serviceName;
        attr = new JoltSessionAttributes();
        attr.setString( attr.APPADDRESS, cfg.getAppAddress() );
        if ( cfg.getRecvTimeout() > 0 ) {
            attr.setInt( attr.RECVTIMEOUT, cfg.getRecvTimeout() );
        }
        if ( cfg.getSendTimeout() > 0 ) {
            attr.setInt( attr.SENDTIMEOUT, cfg.getSendTimeout() );
        }
        if ( cfg.getSessionTimeout() > 0 ) {
            attr.setInt( attr.SESSIONTIMEOUT, cfg.getSessionTimeout() );
        }
        attr.setInt( attr.IDLETIMEOUT, cfg.getIdleTimeout() );

        session = new JoltSession( attr, cfg.getUserName(), cfg.getUserRole(), cfg.getUserPassword(),
                                      cfg.getAppPassword() );
        service = new JoltRemoteService( this.serviceName, session );
    }

    /**
     * setByte
     *
     * @param name String
     * @param val byte
     */
    public void setByte( String name, byte val ) {
        service.setByte( name, val );
    }

    /**
     * setByteItem
     *
     * @param name String
     * @param itemNo int
     * @param val byte
     */
    public void setByteItem( String name, int itemNo, byte val ) {
        service.setByteItem( name, itemNo, val );
    }

    /**
     * setBytes
     *
     * @param name String
     * @param val byte[]
     * @param len int
     */
    public void setBytes( String name, byte[] val, int len ) {
        service.setBytes( name, val, len );
    }

    /**
     * setBytesItem
     *
     * @param name String
     * @param itemNo int
     * @param val byte[]
     * @param len int
     */
    public void setBytesItem( String name, int itemNo, byte[] val, int len ) {
        service.setBytesItem( name, itemNo, val, len );
    }

    /**
     * setDouble
     *
     * @param name String
     * @param val double
     */
    public void setDouble( String name, double val ) {
        service.setDouble( name, val );
    }

    /**
     * setDoubleItem
     *
     * @param name String
     * @param itemNo int
     * @param val double
     */
    public void setDoubleItem( String name, int itemNo, double val ) {
        service.setDoubleItem( name, itemNo, val );
    }

    /**
     * setFloat
     *
     * @param name String
     * @param val float
     */
    public void setFloat( String name, float val ) {
        service.setFloat( name, val );
    }

    /**
     * setFloatItem
     *
     * @param name String
     * @param itemNo int
     * @param val float
     */
    public void setFloatItem( String name, int itemNo, float val ) {
        service.setFloatItem( name, itemNo, val );
    }

    /**
     * setInt
     *
     * @param name String
     * @param val int
     */
    public void setInt( String name, int val ) {
        service.setInt( name, val );
    }

    /**
     * setIntItem
     *
     * @param name String
     * @param itemNo int
     * @param val int
     */
    public void setIntItem( String name, int itemNo, int val ) {
        service.setIntItem( name, itemNo, val );
    }

    /**
     * setShort
     *
     * @param name String
     * @param val short
     */
    public void setShort( String name, short val ) {
        service.setShort( name, val );
    }

    /**
     * setShortItem
     *
     * @param name String
     * @param itemNo int
     * @param val short
     */
    public void setShortItem( String name, int itemNo, short val ) {
        service.setShortItem( name, itemNo, val );
    }

    /**
     * setString
     *
     * @param name String
     * @param val String
     */
    public void setString( String name, String val ) {
        service.setString( name, val );
    }

    /**
     * setStringItem
     *
     * @param name String
     * @param itemNo int
     * @param val String
     */
    public void setStringItem( String name, int itemNo, String val ) {
        service.setStringItem( name, itemNo, val );
    }

    /**
     * getData
     *
     * @return Object
     */
    public Object getData() {
        return this.service;
    }

    /**
     * setData
     *
     * @param obj Object
     */
    public void setData( Object obj ) {
        throw new UnsupportedOperationException( "BasicJoltData does not support the method." );
    }

    /**
     * getOccurrenceCount
     *
     * @param sParamName String
     * @return int
     */
    public int getOccurrenceCount( String sParamName ) {
        return this.service.getOccurrenceCount( sParamName );
    }

    /**
     * getByteDef
     *
     * @param name String
     * @param def byte
     * @return byte
     */
    public byte getByteDef( String name, byte def ) {
        return service.getByteDef( name, def );
    }

    /**
     * getByteItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def byte
     * @return byte
     */
    public byte getByteItemDef( String name, int itemNo, byte def ) {
        return service.getByteItemDef( name, itemNo, def );
    }

    /**
     * getBytesDef
     *
     * @param name String
     * @param def byte[]
     * @return byte[]
     */
    public byte[] getBytesDef( String name, byte[] def ) {
        return service.getBytesDef( name, def );
    }

    /**
     * getBytesItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def byte[]
     * @return byte[]
     */
    public byte[] getBytesItemDef( String name, int itemNo, byte[] def ) {
        return service.getBytesItemDef( name, itemNo, def );
    }

    /**
     * getDoubleDef
     *
     * @param name String
     * @param def double
     * @return double
     */
    public double getDoubleDef( String name, double def ) {
        return service.getDoubleDef( name, def );
    }

    /**
     * getDoubleItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def double
     * @return double
     */
    public double getDoubleItemDef( String name, int itemNo, double def ) {
        return service.getDoubleItemDef( name, itemNo, def );
    }

    /**
     * getFloatDef
     *
     * @param name String
     * @param def float
     * @return float
     */
    public float getFloatDef( String name, float def ) {
        return service.getFloatDef( name, def );
    }

    /**
     * getFloatItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def float
     * @return float
     */
    public float getFloatItemDef( String name, int itemNo, float def ) {
        return service.getFloatItemDef( name, itemNo, def );
    }

    /**
     * getIntDef
     *
     * @param name String
     * @param def int
     * @return int
     */
    public int getIntDef( String name, int def ) {
        return service.getIntDef( name, def );
    }

    /**
     * getIntItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def int
     * @return int
     */
    public int getIntItemDef( String name, int itemNo, int def ) {
        return service.getIntItemDef( name, itemNo, def );
    }

    /**
     * getShortDef
     *
     * @param name String
     * @param def short
     * @return short
     */
    public short getShortDef( String name, short def ) {
        return service.getShortDef( name, def );
    }

    /**
     * getShortItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def short
     * @return short
     */
    public short getShortItemDef( String name, int itemNo, short def ) {
        return service.getShortItemDef( name, itemNo, def );
    }

    /**
     * getStringDef
     *
     * @param name String
     * @param def String
     * @return String
     */
    public String getStringDef( String name, String def ) {
        return service.getStringDef( name, def );
    }

    /**
     * getStringItemDef
     *
     * @param name String
     * @param itemNo int
     * @param def String
     * @return String
     */
    public String getStringItemDef( String name, int itemNo, String def ) {
        return service.getStringItemDef( name, itemNo, def );
    }

    /**
     * destroy
     */
    public void destroy() {
        service.done();
        if( session.isAlive() ) {
            session.endSession();
        }
    }

    /**
     * setServiceName
     *
     * @param serviceName String
     */
    public void setServiceName( String serviceName ) {
        this.serviceName = serviceName;
    }

    /**
     * getServiceName
     *
     * @return String
     */
    public String getServiceName() {
        return this.serviceName;
    }
}
